package com.yuaer.demo.springsparkjobrunnerdemo.runner;

import lombok.SneakyThrows;
import org.springframework.boot.ApplicationArguments;

public abstract class ScriptRunner {
    protected final String taskName;

    /**
     * 使用构造函数强制子类定义taskName
     * @param taskName
     */
    @SneakyThrows
    public ScriptRunner(String taskName) {
        if (taskName == null) {
            throw new IllegalAccessException("taskName is null");
        }
        this.taskName = taskName;
    }
    public String getTaskName() {
        return taskName;
    }

    /**
     * 子类实现自己的run逻辑
     * @param args
     * @throws IllegalAccessException
     */
    public abstract void runTask(ApplicationArguments args) throws IllegalAccessException;

    /**
     * 统一参数取值逻辑
     * @param args
     * @param name
     * @param defaultVal
     * @return
     */
    public String getArg(ApplicationArguments args, String name, String defaultVal) {
        return args.containsOption(name) ? args.getOptionValues(name).get(0) : defaultVal;
    }
}